// page/cart/cart.js
Page({
    data: {
        isHide: 0,
        carts: [],             // 购物车列表
        goods: [],              //推荐列表
        hasList: false,          // 列表是否有数据
        totalPrice: 0          // 总价，初始为0
  },
  
  onLoad: function (options) {
    var app = getApp()
    // console.log(app.globalData.host)
    var uid = wx.getStorageSync('uid');
    var that = this;
    wx.request({
      url: app.globalData.host + '&g=App&m=Cart&a=cart', //仅为示例，并非真实的接口地址
      data: { id: uid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          goods: res.data.g
        })
        that.getTotalPrice();
      }
    });
    // console.log(that.data)

    that.setData({
      isHide: 1
    })   
  },
  onShow() {
    var app = getApp()
    // console.log(app.globalData.host)
    var uid = wx.getStorageSync('uid');
    var that = this;
    wx.request({
      url: app.globalData.host + '&g=App&m=Cart&a=cart', //仅为示例，并非真实的接口地址
      data: { id: uid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          hasList: res.data.c == 'false' ? false : true,
          carts: res.data.d,
          isHide: 1
        })
        that.getTotalPrice();
      }
    });
    // console.log(that.data)

  
  },
  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    var app = getApp()
    var that = this;
      const index = e.currentTarget.dataset.index;
      const id = e.currentTarget.dataset.id;
      let carts = that.data.carts;
      wx.request({
        url: app.globalData.host + '&g=App&m=Cart&a=delcart', //仅为示例，并非真实的接口地址
        data: { id: id},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.status > 0) {
            carts.splice(index, 1);
            that.setData({
              carts: carts,
            });
            if (!carts.length) {
              that.setData({
                hasList: false
              });
            } else {
              that.getTotalPrice();
            }
           
          }
        }
      });

    
  },

  /**
   * 绑定加数量事件
   */
  addCount(e) {
      var app = getApp()
      var that = this;
      const index = e.currentTarget.dataset.index;
      let carts = that.data.carts;
      let num = carts[index].num;
      num ++;
      wx.request({
        url: app.globalData.host + '&g=App&m=Cart&a=addcount', //仅为示例，并非真实的接口地址
        data: { id: carts[index].id, num: num },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status > 0) {
            that.setData({
              carts: carts
            })
          }
        }
      });
      carts[index].num = num;
      that.setData({
          carts: carts
      });
      that.getTotalPrice();
  },

  /**
   * 绑定减数量事件
   */
  minusCount(e) {
      var app = getApp()
      var that = this;
      const index = e.currentTarget.dataset.index;
      let carts = that.data.carts;
      let num = carts[index].num;
      if (num <= 1) {
          return false;
      }
      num--;
      
      wx.request({
        url: app.globalData.host + '&g=App&m=Cart&a=minuscount', //仅为示例，并非真实的接口地址
        data: { id: carts[index].id, num:num},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status>0){
            that.setData({
              carts: carts
            })
          }
        
        
        }
      });
      carts[index].num = num;
      that.getTotalPrice();
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
      let carts = this.data.carts;                  // 获取购物车列表
      let total = 0;
      for (let i = 0; i < carts.length; i++) {         // 循环列表得到每个数据
        //  if (carts[i].selected) {                     // 判断选中才会计算价格
              total += carts[i].num * carts[i].price;
        //  }
      }
      this.setData({                                // 最后赋值到data中渲染到页面
          totalPrice: total.toFixed(2)
      });
  },
  tz:function (){
    wx.switchTab({
      url: '/page/category/category',
    })
  }  
})